import json
from django_redis import get_redis_connection


def save_image_cache(uuid, x, y):
	"""存入正确的x y坐标"""
	redis_conn = get_redis_connection('imageCache')
	redis_conn.hset("image_code", uuid, json.dumps({"x": x, "y": y, "is_used": 0}))


def get_image_cache(uuid):
	"""取出对应的图片坐标"""
	redis_conn = get_redis_connection('imageCache')
	data = redis_conn.hget("image_code", uuid)
	if data:
		redis_conn.hdel("image_code", uuid)
		data = json.loads(data)
		return data["x"], data["y"], True
	else:
		return 0, 0, False


def save_image_cache_success(uuid):
	"""存入正确验证后的uuid"""
	redis_conn = get_redis_connection('imageCache')
	redis_conn.hset("image_code_success_key", uuid, "1")


def get_image_cache_success(uuid):
	"""取出验证后的uuid"""
	redis_conn = get_redis_connection('imageCache')
	data = redis_conn.hget("image_code_success_key", uuid)
	if data:
		return True
	else:
		return False


def remove_image_cache_success(uuid):
	"""成功验证后 删除uuid key"""
	redis_conn = get_redis_connection('imageCache')
	redis_conn.hdel("image_code_success_key", uuid)


def get_qrcode_cache_success(uuid):
	"""取出验证后的uuid"""
	redis_conn = get_redis_connection('imageCache')
	data = redis_conn.hget("qrcode_success_key", uuid)
	if data:
		return True
	else:
		return False


def remove_qrcode_cache_success(uuid):
	"""成功验证后 删除uuid key"""
	redis_conn = get_redis_connection('imageCache')
	redis_conn.hdel("qrcode_success_key", uuid)


def get_user_status(host):
	"""获取用户状态"""
	redis_conn = get_redis_connection('imageCache')
	return True if redis_conn.get(host) else False


def set_user_status(host):
	"""设置用户状态"""
	redis_conn = get_redis_connection('imageCache')
	redis_conn.set(host, "", 600)


if __name__ == "__main__":
	from django_redis import get_redis_connection
	red = get_redis_connection('imageCache')
	a = red.keys("*")
	print(a)
	print(red.hget("image_code", "9b718277-3dab-4d8b-a54d-da0898b225e4"))
	print(red.get("11"))

